`timescale 1ns / 1ns
module riscv_aes_tb;
  	wire complete,ready;
  	wire [31:0] rfout;
  	reg clk,rst,writeim,rfwen_ext,start;
  	reg [4:0] imaddr,rfaddr_ext;
  	reg [31:0] imin,rfin_ext;  
  	riscv_aes  u_riscv_aes(rfout,complete,ready,clk,rst,writeim,imaddr,imin,rfwen_ext,rfaddr_ext,rfin_ext,start);  
  	//clock generation				   
	initial clk = 1;
	always #5 clk = ~clk;	
	initial 
	  	begin		     	
		    rst=1;
		    writeim=0;
		    rfwen_ext=0;
		    start=0;		    
		#2  rst=0; //test reset
		#20 rst=1;
			writeim=1;//load instruction into instruction memory. //指令写入使能
		    imaddr=5'd0;//指令写入指令存储器的地址0.
		 	imin=32'b0000001_00000_00000_010_00000_0001011;//写入指令1
//instruction 1:load_key rs1,function:load cipher key[31:0] from RF into AES IP.//指令1：将加密密钥[31:0]从RF加载到AES IP
		#10 imaddr=5'd1;//地址1
		 	imin=32'b0000001_00000_00001_010_00000_0001011;//写入指令2
//instruction 2:load_key rs1,function:load cipher key[63:32] from RF into AES IP.//指令2：将加密密钥[63:32]从RF加载到AES IP
		#10 imaddr=5'd2;//地址2
		 	imin=32'b0000001_00000_00010_010_00000_0001011;//写入指令3
//instruction 3:load_key rs1,function:load cipher key[95:64] from RF into AES IP.//指令3：将加密密钥[95:64]从RF加载到AES IP 
		#10 imaddr=5'd3;//地址3
		 	imin=32'b0000001_00000_00011_010_00000_0001011;//写入指令4
//instruction 4:load_key rs1,function:load cipher key[127:96] from RF into AES IP.//指令4：将加密密钥[127:96]从RF加载到AES IP
		#10 imaddr=5'd4;//地址4
		 	imin=32'b0000010_00000_00100_010_00000_0001011;//写入指令5
//instruction 5:load_data rs1,function:load plaintext[31:0] from RF into AES IP.//指令5：将明文从RF加载到AES IP
		#10 imaddr=5'd5;//地址5
		    imin=32'b0000010_00000_00101_010_00000_0001011;//写入指令6
//instruction 6:load_data rs1,function:load plaintext[63:32] from RF into AES IP.
		#10 imaddr=5'd6;//地址6
		 	imin=32'b0000010_00000_00110_010_00000_0001011;//写入指令7
//instruction 7:load_data rs1,function:load plaintext[95:64] from RF into AES IP.
		#10 imaddr=5'd7;//地址7
		 	imin=32'b0000010_00000_00111_010_00000_0001011;//写入指令8
//instruction 8:load_data rs1,function:load plaintext[127:96] from RF into AES IP.
		#10 imaddr=5'd8;//地址8
			imin=32'b0000011_00000_00000_000_00000_0001011;//写入指令9
//instruction 9:start_encrypt,function:start AES IP to execute encryption.//指令9：开始加密
		#10 imaddr=5'd9;//地址9
			imin=32'b0000111_00000_00000_000_01101_0001011;//写入指令10：条件转移指令：加密完成跳转到结果保存指令
//instruction 10:branch_if_ready rd,function:if encryption complete,
//goto "store_result rd" instruction to store encryption result into RF.   
		#10	imaddr=5'd10;
			imin=32'b0000000_00000_00000_000_00000_0001011;//写入指令11：空操作，等待加密完成
//instruction 11:no_operation,function:no operation,
//used to solve branch dependence.
		#10 imaddr=5'd11;
		 	imin=32'b0000110_00000_00000_000_01001_0001011;//写入指令12：无条件转移指令，跳转到条件转移指令
//instruction 12:branch rd,function:jump to "branch_if_ready rd" instruction.	
		#10 imaddr=5'd12;
			imin=32'b0000000_00000_00000_000_00000_0001011;//写入指令13：空操作
//instruction 13:no_operation,function:no operation,
//used to solve branch dependence.
		#10 imaddr=5'd13;
		   	imin=32'b0000101_00000_00000_100_10100_0001011;//写入指令14：加密完成保存结果[31:0]
//instruction 14:store_result rd,function:to store result[31:0] into RF,addr=20.	
		#10 imaddr=5'd14;
		   	imin=32'b0000101_00000_00000_100_10101_0001011;//写入指令15：加密完成保存结果[63:32]
//instruction 15:store_result rd,function:to store result[63:32] into RF,addr=21.
		#10 imaddr=5'd15;
		   	imin=32'b0000101_00000_00000_100_10110_0001011;//写入指令16：加密完成保存结果[95:64]
//instruction 16:store_result rd,function:to store result[95:64] into RF,addr=22.
		#10 imaddr=5'd16;
		   	imin=32'b0000101_00000_00000_100_10111_0001011;//写入指令17：加密完成保存结果[127:96]
//instruction 17:store_result rd,function:to store result[127:96] into RF,addr=23.
		#10 imaddr=5'd17;
		  	imin=32'b0000010_00000_01000_010_00000_0001011;//写入指令18：将密文1从RF加载到AES IP
//instruction 18:load_data rs1,function:load ciphertext[31:0] from RF into AES IP.
		#10 imaddr=5'd18;
		  	imin=32'b0000010_00000_01001_010_00000_0001011;//写入指令19
//instruction 19:load_data rs1,function:load ciphertext[63:32] from RF into AES IP.
		#10 imaddr=5'd19;
		  	imin=32'b0000010_00000_01010_010_00000_0001011;//写入指令20
//instruction 20:load_data rs1,function:load ciphertext[95:64] from RF into AES IP.
		#10 imaddr=5'd20;
		  	imin=32'b0000010_00000_01011_010_00000_0001011;//写入指令21
//instruction 21:load_data rs1,function:load ciphertext[127:96] from RF into AES 	//IP.
		#10 imaddr=5'd21;
			imin=32'b0000100_00000_00000_000_00000_0001011;//写入指令22：开始解密
//instruction 22:start_decrypt,function:start AES IP to execute decryption. 
		#10 imaddr=5'd22;
			imin=32'b0000111_00000_00000_000_11010_0001011;//写入指令23：条件转移指令：解密完成跳转到结果保存指令
//instruction 23:branch_if_ready rd,function:if decryption complete,
//goto "store_result rd" instruction to store decryption result into RF.   
		#10	imaddr=5'd23;
			imin=32'b0000000_00000_00000_000_00000_0001011;//写入指令24：空操作
//instruction 24:no_operation,function:no operation,
//used to solve branch dependence. 
		#10 imaddr=5'd24;
			imin=32'b0000110_00000_00000_000_10110_0001011;//写入指令25：无条件转移指令
//instruction 25:branch rd,function:jump to "branch_if_ready rd" instruction.
		#10 imaddr=5'd25;
			imin=32'b0000000_00000_00000_000_00000_0001011;//写入指令26：空操作
//instruction 26:no_operation,function:no operation,
//used to solve branch dependence.
		#10 imaddr=5'd26;
		   	imin=32'b0000101_00000_00000_100_11000_0001011;//写入指令27：解密完成保存结果[31:0]
//instruction 27:store_result rd,function:to store result[31:0] into RF,addr=24.	
		#10 imaddr=5'd27;
		   	imin=32'b0000101_00000_00000_100_11001_0001011;//写入指令28
//instruction 28:store_result rd,function:to store result[63:32] into RF,addr=25.
		#10 imaddr=5'd28;
		   	imin=32'b0000101_00000_00000_100_11010_0001011;//写入指令29
//instruction 29:store_result rd,function:to store result[95:64] into RF,addr=26.
		#10 imaddr=5'd29;
		   	imin=32'b0000101_00000_00000_100_11011_0001011;//写入指令30
//instruction 30:store_result rd,function:to store result[127:96] into RF,addr=27.	
		#10 imaddr=5'd30;
			imin=32'b1111111_00000_00000_000_00000_0001011;//写入指令31：停机
//instruction 31:halt,function:to stop instruction execution.
		#10 imaddr=5'd31;
			imin=32'b0000000_00000_00000_000_00000_0001011;//写入指令32：空操作
//instruction 32:no_operation,function:no operation,used to avoid error operation.
		#10 writeim=0;//关闭指令存储器
		#20 rfwen_ext=1;//load cipherkey,plaintext and ciphertext from outside into RF.//装载子密钥、明文、密文使能信号
		    rfaddr_ext=5'd0;
		    rfin_ext=32'h09cf4f3c;//load cipher key[31:0] from outside into RF.//子密钥 地址0	
		#10 rfaddr_ext=5'd1;
		    rfin_ext=32'habf71588;//load cipher key[63:32] from outside into RF.//地址1
		#10 rfaddr_ext=5'd2;
		    rfin_ext=32'h28aed2a6;//load cipher key[95:64] from outside into RF.//地址2
		#10 rfaddr_ext=5'd3;
		    rfin_ext=32'h2b7e1516;//load cipher key[127:96] from outside into RF.//地址3
		#10 rfaddr_ext=5'd4;
		    rfin_ext=32'he0370734;//load plaintext[31:0] from outside into RF.//明文 地址4
		#10 rfaddr_ext=5'd5;
		    rfin_ext=32'h313198a2;//load plaintext[63:32] from outside into RF.//地址5
		#10 rfaddr_ext=5'd6;
		    rfin_ext=32'h885a308d;//load plaintext[95:64] from outside into RF.//地址6
		#10 rfaddr_ext=5'd7;
		    rfin_ext=32'h3243f6a8;//load plaintext[127:96] from outside into RF.//地址7
		#10 rfaddr_ext=5'd8;
		    rfin_ext=32'h196a0b32;//load ciphertext[31:0] from outside into RF.//密文 地址8	
		#10 rfaddr_ext=5'd9;
		    rfin_ext=32'hdc118597;//load ciphertext[63:32] from outside into RF.//地址9
		#10 rfaddr_ext=5'd10;
		    rfin_ext=32'h02dc09fb;//load ciphertext[95:64] from outside into RF.//地址10
		#10 rfaddr_ext=5'd11;
		    rfin_ext=32'h3925841d;//load ciphertext[127:96] from outside into RF.//地址11
		#10 rfwen_ext=0;//关闭RF装载使能
		#20 start=1;//start instruction execution;//开始加密
		#10 start=0;			
		#600 rfaddr_ext=5'd20;//read results of enceyption and decryption from RF to outside. //保存加密1结果到RF 地址20
		#10 rfaddr_ext=5'd21;//保存加密2结果到RF 地址21
		#10 rfaddr_ext=5'd22;//保存加密3结果到RF 地址22
		#10 rfaddr_ext=5'd23;//保存加密4结果到RF 地址23
		#10 rfaddr_ext=5'd24;//保存解密1结果到RF 地址24
		#10 rfaddr_ext=5'd25;//保存解密2结果到RF 地址25
		#10 rfaddr_ext=5'd26;//保存解密3结果到RF 地址26
		#10 rfaddr_ext=5'd27;//保存解密4结果到RF 地址27
		#10 $finish;		
	  end
	  
	  	//initial begin
		//#500000000  $finish;
  
       //	end
  
	initial begin
	$fsdbDumpfile("wave.fsdb");
	$fsdbDumpvars            ;
	$fsdbDumpMDA             ;
	end
endmodule
